<?php
/*
*
* MARCO VOEGELI 31.12.2005
* www.voegeli.li
*
* This class provides one central database-connection for
* al your php applications. Define only once your connection
* settings and use it in all your applications.
*
*
*/

  
 class Database
 { // Class : begin
 var $host;         //Hostname, Server
 var $password; 	//Passwort MySQL
 var $user; 		//User MySQL
 var $database; 	//Datenbankname MySQL
 var $link;
 var $query;
 var $result;
 var $rows;
 
 function Database()
 { // Method : begin
 //Konstruktor
 
 
 
 // ********** DIESE WERTE ANPASSEN **************
 // ********** ADJUST THESE VALUES HERE **********
  include "database_config.php";
  $this->host = $DB_HOST;                  //          <<---------
  $this->password = $DB_PASS;           //          <<---------
  $this->user = $DB_USER;                   //          <<---------
  $this->database = $DB_NAME;           //          <<---------
  $this->rows = 0;
 
 // **********************************************
 // **********************************************
 
 
  
 } // Method : end
 
 /*
		$selects = array('table_name.column1', 'table_name.column2'...)
		$froms = array(('table_name1' =>'alias1'), ('table_name2' => 'alias2') ...)
		$joins = array('table_name1' => array('join_type' => 'LEFT JOIN',
											   'ON'		  => 'table_name.column'),
						table_name2' => array('join_type' => 'LEFT JOIN',
											  'ON'		  => 'table_name.column'),)
		$where_clause = array('null' => array('left_condition', 'operator', right_condition), 'AND/OR' => array('left_condition', 'operator', right_condition)...)
		$order_by = 'text'
		$group_by = 'text'
		...
	*/
	
	public static function doQuery($selects, $froms, $joins=null, $where_clause=null, $order_by=null, $group_by=null, $limit=null)
	{
		$db = new Database();
		$query = 'SELECT ';

		// select
		foreach ($selects as $select)
			if ($select == end($selects))
				$query .= $select.' ';
			else
				$query .= $select.',';
				
		// from
		$query .= 'FROM ';
		$index = 0;
		foreach ($froms as $from) {
			$query .= $from['table_name'];
			if ($from['alias'])
				$query .= ' AS '.$from['alias'];

			if ($index == count($froms) - 1)
					$query .= ' ';
			else
				$query .= ',';
			$index++;
		}
		
		if ($joins)
			foreach ($joins as $table_name => $join_data) {
				$query .= $join_data['join_type'].' ';
				$query .= 'ON '.$join_data['ON'].' ';
			}
		
		if ($where_clause) {
			$query .= 'WHERE ';
			foreach ($where_clause as $data) {
				if ($data[0])
					$query .= ' '.$data[0].' ';
				$query .= $data[1].' '.$data[2].' '.$data[3];
			}
		}
		
		$index = 0;
		if ($order_by) {
			$query .= ' ODER BY ';
			foreach ($order_by as $order) {
				$index ++;
				$query .= $order_by;
				if ($index == count($order_by))
					$query .= ' ';
				else
					$query .= ',';
			}
		} 
		$index = 0;
		if ($group_by) {
			$query .= ' GROUP BY '; 
			foreach ($group_by as $group) {
				$index ++;
				$query .= $group.',';
				if ($index == count($order_by))
					$query .= ' ';
				else
					$query .= ',';
			}
		}
		if ($limit)
			$query .= ' LIMIT '.$limit[0].','.$limit[1];
			
		$db->query($query);
		$result = $db->result;
		$result = $db->fetch_array($result);
		return $result;
	}

 
 function OpenLink()
 { // Method : begin
  $this->link = @mysql_connect($this->host,$this->user,$this->password) or die (print "Class Database: Error while connecting to DB (link)");
 } // Method : end
 
 function SelectDB()
 { // Method : begin
 
 @mysql_select_db($this->database,$this->link) or die (print "Class Database: Error while selecting DB");
  
 } // Method : end
 
 function CloseDB()
 { // Method : begin
 mysql_close();
 } // Method : end
 
 function Query($query)
 { // Method : begin
 $this->OpenLink();
 $this->SelectDB();
 $this->query = $query;
 $this->result = mysql_query($query,$this->link) or die (print 'Query: '.$query.'. Error: '.mysql_error());
 
// $rows=mysql_affected_rows();

if(preg_match("#SELECT#",$query))
{
 $this->rows = mysql_num_rows($this->result);
}
 
 $this->CloseDB();
 } // Method : end	


 public function fetch_array()
 {
	$result = array();
	while ($r = mysql_fetch_array($this->result))
		$result[] = $r;
	return $result;
 }
  
} // Class : end
 
?>
